package com.uniandes.edu.co.interfaz.persona;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;

import com.uniandes.edu.co.dto.Persona;
import com.uniandes.edu.co.dto.Tsp;
import com.uniandes.edu.co.excepciones.ExcepcionExiste;
import com.uniandes.edu.co.excepciones.ExcepcionVacio;
import com.uniandes.edu.co.interfaz.mundo.Principal;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ModificarPersona extends javax.swing.JDialog {
	private Principal principal;
	private JLabel jLabel1;
	private JButton jButton1;
	private JComboBox jComboBox1;
	private JButton jButton2;
	private JLabel jLabel4;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JTextField jTextField3;
	private JTextField jTextField2;

	/**
	* Auto-generated main method to display this JDialog
	*/
	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				ModificarPersona inst = new ModificarPersona(frame);
				inst.setVisible(true);
			}
		});
	}*/
	
	public ModificarPersona(Principal id) {
		super(id, true);
        principal = id;
        setTitle( "Modificar Persona" );
        initGUI();
	}
	
	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					jLabel1 = new JLabel();
					getContentPane().add(jLabel1);
					jLabel1.setText("Modificar Persona");
					jLabel1.setBounds(46, 12, 311, 26);
					jLabel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
					jLabel1.setFont(new java.awt.Font("Tahoma",1,14));
					jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
	                jLabel1.setHorizontalTextPosition(SwingConstants.CENTER);
				}
				{
					jButton1 = new JButton();
					getContentPane().add(jButton1);
					jButton1.setText("Modificar");
					jButton1.setBounds(46, 208, 124, 23);
					jButton1.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try{
								String idSeleccionado=(String)jComboBox1.getSelectedItem();
								Tsp.getSingleton().modificarPersona(idSeleccionado, jTextField2.getText(), jTextField3.getText(), Tsp.getSingleton().getPersonas());
								JOptionPane.showMessageDialog(null, "Se ha modificado la persona correctamente");
								dispose();
							} catch (ExcepcionVacio e) {
								// TODO Auto-generated catch block
								JOptionPane.showMessageDialog(null, e.getMessage());
							} catch (ExcepcionExiste e) {
								// TODO Auto-generated catch block
								JOptionPane.showMessageDialog(null, e.getMessage());
							}
							//JOptionPane.showMessageDialog(null, jTextField1.getText());
							//TODO add your code for jButton1.actionPerformed
						}
					});
				}
				{
					jTextField2 = new JTextField();
					getContentPane().add(jTextField2);
					jTextField2.setBounds(188, 100, 169, 23);
				}
				{
					jTextField3 = new JTextField();
					getContentPane().add(jTextField3);
					jTextField3.setBounds(188, 147, 169, 23);
				}
				{
					jLabel2 = new JLabel();
					getContentPane().add(jLabel2);
					jLabel2.setText("Id Persona");
					jLabel2.setBounds(46, 66, 124, 16);
				}
				{
					jLabel3 = new JLabel();
					getContentPane().add(jLabel3);
					jLabel3.setText("Nombre de la Persona");
					jLabel3.setBounds(46, 107, 130, 16);
				}
				{
					jLabel4 = new JLabel();
					getContentPane().add(jLabel4);
					jLabel4.setText("Rol");
					jLabel4.setBounds(46, 154, 124, 16);
				}
				{
					jButton2 = new JButton();
					getContentPane().add(jButton2);
					jButton2.setText("Cancelar");
					jButton2.setBounds(188, 208, 169, 23);
					jButton2.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							dispose();
						}
					});
				}
				{
					List <Persona> personas=Tsp.getSingleton().getPersonas();
					List <String> idPersonas=new ArrayList<String>();
					idPersonas.add("Seleccione...");
					for (Persona per : personas) {
						idPersonas.add(per.getIdPersona());
					}
					ComboBoxModel jComboBox1Model = 
						new DefaultComboBoxModel(
								idPersonas.toArray());
					jComboBox1 = new JComboBox();
					getContentPane().add(jComboBox1);
					jComboBox1.setModel(jComboBox1Model);
					jComboBox1.setBounds(188, 63, 169, 23);
					jComboBox1.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							try{
								String idSeleccionado=(String)jComboBox1.getSelectedItem();
								if(idSeleccionado.equalsIgnoreCase("Seleccione...")){
									jTextField2.setText(null);
									jTextField3.setText(null);
								}else{
									Persona persona=Tsp.getSingleton().consultarPersona(idSeleccionado, Tsp.getSingleton().getPersonas());
									if(persona!=null){
										jTextField2.setText(persona.getNombre());
										jTextField3.setText(persona.getRol());
									}
								}
							} catch (ExcepcionVacio e) {
								// TODO Auto-generated catch block
								JOptionPane.showMessageDialog(null, e.getMessage());
							}
						}
					});
				}
			}
			setSize(400, 300);
			this.setLocationRelativeTo(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
